package com.cskaoyan.config3;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 注册一个DruidDataSource 3.0
 * @author stone
 * @date 2022年1月4日16:14:09
 */
@Configuration
public class DruidDataSourceConfiguration {

    //@Autowired
    DataSourceProperties properties;

    //通常通过有参构造方法来引入对应成员变量
    //如果组件中没有无参构造方法，会使用有参构造 → 形参会自动按照类型从容器中取出组件
    public DruidDataSourceConfiguration(DataSourceProperties properties) {
        this.properties = properties;
    }

    @Bean
    public DruidDataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(properties.getDriver());
        dataSource.setUrl(properties.getUrl());
        dataSource.setUsername(properties.getUsername());
        dataSource.setPassword(properties.getPassword());
        return dataSource;
    }
}
